package homework;

import java.util.Scanner;

/**
 * @author DCSGO
 * @version 1.0
 * 输入用户名 密码 邮箱
 * 如果信息录入正确 则提示注册成功
 * 否则生成异常对象
 * 要求:
 * 1.用户名长度为2或3或4
 * 2.密码的长度为6,要求全是数字
 * 3.邮箱中包含@和. 并且@在.的前面
 */
public class Homework02 {
    //字符串是否由纯数字组成
    public static boolean isDigital(String str) {
        char[] chars = str.toCharArray();
        for (char ch : chars) {
            if (!(Character.isDigit(ch))) {
                return false;
            }
        }
        return true;
    }

    public static void userRegister(String name, String password, String mail) {
        if (!(name.length() >= 2 && name.length() <= 4)) {
            throw new RuntimeException("用户名长度必需为2或3或4");
        }
        if (!(password.length() == 6 && isDigital(password))) {
            throw new RuntimeException("密码长度必须为6且由数字组成");
        }
        int i = mail.indexOf('@');
        int j = mail.indexOf('.');
        if (!(0 < i && i < j)) {
            throw new RuntimeException("邮箱中必须包含@和. 并且@在.的前面");
        }
        System.out.println("注册成功!");
    }

    public static void login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名: ");
        String userName = scanner.next();
        if (!(userName.length() >= 2 && userName.length() <= 4)) {
            throw new RuntimeException("用户名长度必须为2或3或4!");
        }

        //可读性差
        System.out.println("请输入密码: ");
        String password = scanner.next();
        if (password.length() == 6) {
            char[] chars = password.toCharArray();
            for (char ch : chars) {
                if (!Character.isDigit(ch)) {
                    throw new RuntimeException("输入的密码必须由数字组成!");
                }
            }
        } else {
            throw new RuntimeException("密码长度必须为6!");
        }

        System.out.println("请输入邮箱: ");
        String mail = scanner.next();
        int pos1 = mail.indexOf('@');
        int pos2 = mail.indexOf('.');
        if (!(pos1 < pos2 && pos1 >= 0)) {
            throw new RuntimeException("邮箱中必须包含@和. 并且@在.的前面!");
        }
    }

    public static void main(String[] args) {
//        try {
//            login();
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }

        try {
            String name="12";
            String password="123457";
            String mail="1d.sfs@qqcom";
            userRegister(name,password,mail);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
